package com.resifind.www.dao.impl;

import com.resifind.www.dao.RecordDao;
import com.resifind.www.dao.mapper.Mappers;
import com.resifind.www.po.Record;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class RecordDaoImpl implements RecordDao {

    @Override
    public List<Record> queryRecordByClientId(int clientId) {
        //language=MySQL
        String sql = "select * from resi_find.record where client_id = ? order by date desc";
        Object[] params = {clientId};
        return Mappers.recordMapper(sql, params);
    }

    @Override
    public int queryTotalDaysByClientId(int clientId) {
        //language=MySQL
        String sql = "select count(*) from resi_find.record where client_id = ?";
        Object[] params = {clientId};
        List<Integer> query = JdbcUtils.query(sql, params, rs -> rs.getInt(1));
        return query.getFirst();
    }

    @Override
    public boolean addRecord(int clientId, int bonusCoin, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.record (client_id, bonus_coin) values (?,?)";
        Object[] params = {clientId, bonusCoin};
        return JdbcUtils.update(sql, params, conn);
    }
}
